package pfc.taller.servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;

import org.apache.log4j.Logger;

import pfc.taller.comun.Constantes;
import pfc.taller.log.Log;

public class ServletPDF extends HttpServlet
{
	private static final long serialVersionUID = 6899763285623903197L;
	Logger log = Log.getInstance();
	
	protected void service(HttpServletRequest request, HttpServletResponse response) 
	throws ServletException, IOException
	{
		
		String factura							= request.getParameter("factura");
		ServletOutputStream servletOutputStream = response.getOutputStream();
		HashMap<String, String> mapa 			= new HashMap<String, String>();
		Connection conn 						= null;
		Boolean continuar						= Boolean.TRUE;
		mapa.put("ID_FACTURA", factura);
		
		try 
		{
		      Class.forName( Constantes.DRIVERMYSQL );
		      conn = DriverManager.getConnection(Constantes.CADENABBDD, Constantes.USUARIOBBDD, Constantes.CLAVEBBDD);
		}
		catch (Exception e) 
		{
		      log.trace("Se ha producido un error al cargar el driver MySql", e);
		      continuar = Boolean.FALSE;
		}
		
		if(continuar)
		{
			try
			{	
				JasperReport report 	= JasperCompileManager.compileReport( Constantes.JRXMLFACTURA );
				JasperPrint print 		= JasperFillManager.fillReport(report, mapa, conn );
				JasperExportManager.exportReportToPdfStream(print, servletOutputStream);
				byte[] fichero 			= JasperRunManager.runReportToPdf (report, mapa, conn);
	
				response.setContentType ("application/pdf");
				response.setHeader ("Content-disposition", "inline; filename=informeDemo.pdf");
				response.setHeader ("Cache-Control", "max-age=30");
				response.setHeader ("Pragma", "No-cache");
				response.setDateHeader ("Expires", 0);
				response.setContentLength (fichero.length);
				
				 
				servletOutputStream.write (fichero, 0, fichero.length);
				servletOutputStream.flush ();
				servletOutputStream.close (); 
				
				//Con esto sale pero en un archivo
				/**
				JasperReport report = JasperCompileManager.compileReport( Constantes.JRXMLFACTURA );
				JasperPrint print = JasperFillManager.fillReport(report, mapa, conn );
				JasperExportManager.exportReportToPdfFile(print, "C:\\prueba.pdf");
				response.setContentType("application/pdf");
			    servletOutputStream.flush();
			    servletOutputStream.close();
			    */
		    }
		    catch (JRException e)
		    {
			    log.trace("Problemas al mostrar la salida del pdf", e);
			}
	
		}
	}	
} 